#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    init();
    connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),
            this, SLOT(treeItemChange(QTreeWidgetItem*,int)));
}

void Widget::init()
{
    ui->treeWidget->clear();
    QTreeWidgetItem *group1=new QTreeWidgetItem(ui->treeWidget);
    group1->setText(0,"group1");
    group1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|
                     Qt::ItemIsSelectable);
    group1->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subItem11=new QTreeWidgetItem(group1);
    subItem11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|
                        Qt::ItemIsSelectable);
    subItem11->setText(0,"subItem11");
    subItem11->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subItem12=new QTreeWidgetItem(group1);
    subItem12->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|
                        Qt::ItemIsSelectable);
    subItem12->setText(0,"subItem12");
    subItem12->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subItem13=new QTreeWidgetItem(group1);
    subItem13->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|
                        Qt::ItemIsSelectable);
    subItem13->setText(0,"subItem13");
    subItem13->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subItem14=new QTreeWidgetItem(group1);
    subItem14->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|
                        Qt::ItemIsSelectable);
    subItem14->setText(0,"subItem14");
    subItem14->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subItem15=new QTreeWidgetItem(group1);
    subItem15->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|
                        Qt::ItemIsSelectable);
    subItem15->setText(0,"subItem15");
    subItem15->setCheckState(0,Qt::Unchecked);
}

void Widget::treeItemChange(QTreeWidgetItem *item, int column)
{
    QString itemText=item->text(0);
    if(Qt::Checked==item->checkState(0))
    {
        QTreeWidgetItem *parent=item->parent();
        int count=item->childCount();
        if(count>0)
        {
            for(int i=0;i<count;i++)
            {
                item->child(i)->setCheckState(0,Qt::Checked);
            }
        }
        else
        {
            updateparentItem(item);
        }
    }
    else if(Qt::Checked==item->checkState(0))
    {
        int count=item->childCount();
        if(count>0)
        {
            for(int i=0;i<count;i++)
            {
                item->child(i)->setCheckState(0,Qt::Unchecked);
            }
        }
        else
        {
            updateparentItem(item);
        }
    }
}

void Widget::updateparentItem(QTreeWidgetItem *item)
{
    QTreeWidgetItem *parent=item->parent();
    if(parent==NULL)
    {
        return;
    }
    int selectedCount=0;
    int childcount=item->childCount();
    for(int i=0;i<childcount;i++)
    {
        QTreeWidgetItem *childItem=parent->child(i);
        if(childItem->checkState(0)==Qt::Checked)
        {
            selectedCount++;
        }
    }

    if(selectedCount<=0)
    {
        parent->setCheckState(0,Qt::Unchecked);
    }
    else if(selectedCount>0&&selectedCount<childcount)
    {
        parent->setCheckState(0,Qt::PartiallyChecked);
    }
    else if(selectedCount==childcount)
    {
        parent->setCheckState(0,Qt::Checked);
    }
}


Widget::~Widget()
{
    delete ui;
}
